﻿using System;
using System.Linq;
using System.Linq.Dynamic.Core;
using Abp.Linq.Extensions;
using System.Collections.Generic;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using AppFramework.Version.Dtos;
using Abp.Application.Services.Dto;
using AppFramework.Authorization;
using Abp.Authorization;
using Microsoft.EntityFrameworkCore;
using AppFramework.Demo.Dtos;
using Microsoft.AspNetCore.Mvc;
using AppFramework.DinosaursParkMes.Process.DTO;
using AppFramework.DinosaursParkMes.WorkOrder;

namespace AppFramework.DinosaursParkMes.Process
{
    public class ProcessAppService : AppFrameworkAppServiceBase, IProcessAppService
    {
        private readonly IRepository<ProcessModel> _repository;
        private readonly IWorkOrderAppService workOrderApp;
        public ProcessAppService(IRepository<ProcessModel> repository = null, IWorkOrderAppService workOrderApp = null)
        {
            _repository = repository;
            this.workOrderApp = workOrderApp;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("AddProcess")]
        public async Task<int> AddProcess(ProcessAdd dto)
        {
            //ProcessModel processModel = new ProcessModel(dto.Processname, dto.Processcode, dto.Processstate, dto.Processremark, dto.Processexplain);
            //ProcessModel processModel1 = await _repository.InsertAsync(processModel);
            //return 1;
            var dtos = ObjectMapper.Map<ProcessModel>(dto);
            var modelname = "Process";
            dtos.Processcode = await workOrderApp.GetCode(modelname);
            _repository.Insert(dtos);
            return 1;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name = "ids" ></ param >
        /// < returns ></ returns >
        /// < exception cref="NotImplementedException"></exception>
        [HttpDelete("DelProcess")]
        public async Task<int> DeleteProcess(string ids)
        {
            string[] idarr = ids.Split(',');
            foreach (var item in idarr)
            {
                var list = _repository.FirstOrDefault(x => x.Id == int.Parse(item));
                if (list != null)
                {
                    _repository.DeleteAsync(list);
                }
            }
            return 1;
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name = "id" ></ param >
        /// < returns ></ returns >
        /// < exception cref="NotImplementedException"></exception>
        [HttpGet("GetProcessFind")]
        public dynamic GetProcessFind(int id)
        {
            var list = _repository.GetAllList().Where(x => x.Id == id).FirstOrDefault();
            return list;
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet("GetProcessShow")]
        public async Task<PagedResultDto<ProcessDTO>> GetProcessShow(ProcessDTOs dto)
        {
             PagedResultDto<ProcessDTO> dtos = new  PagedResultDto<ProcessDTO>();
            var list = from x in _repository.GetAll() where x.IsDeleted==false
                       select new ProcessDTO
                       {
                           Id= x.Id,
                           Processname= x.Processname,
                           Processcode= x.Processcode,
                           Processstate= x.Processstate,
                           Processremark= x.Processremark,
                           Processexplain= x.Processexplain
                       };
            if (!string.IsNullOrEmpty(dto.ProcessnameCode))
            {
                list = list.Where(m => m.Processcode == dto.ProcessnameCode || m.Processname == dto.ProcessnameCode);
            }
            if (!string.IsNullOrEmpty(dto.Processstate))
            {
                list = list.Where(m => m.Processstate == dto.Processstate);
            }

            dtos.TotalCount = list.Count();
            //晚点toList提高我们的性能
            dtos.Items = await list.OrderByDescending(p => p.Id).Skip((dto.pageindex - 1) * dto.pagesize).Take(dto.pagesize).ToListAsync();
            return dtos;

        }

        ///// <summary>
        ///// 修改
        ///// </summary>
        ///// <param name="dto"></param>
        ///// <returns></returns>
        ///// <exception cref="NotImplementedException"></exception>
        [HttpPut("PutProcessUpdate")]
        public async Task<int> PutUpdateProcess(ProcessAdd dto)
        {
            await _repository.UpdateAsync(ObjectMapper.Map<ProcessModel>(dto));
            return 1;
        }

        public dynamic GetShow()
        {
            var list = _repository.GetAllList();
            return list;
        }


    }
}
